package team.seekers.hula.course.course.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.hula.common.annotation.ApiResponseBody;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.course.course.entity.vo.CourseManageInfoVO;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.course.course.dao.CourseDao;
import team.seekers.hula.course.course.entity.dao.CourseDO;
import team.seekers.hula.course.course.entity.dto.CourseCreateDTO;
import team.seekers.hula.course.course.entity.dto.CourseListReqDTO;
import team.seekers.hula.course.course.entity.dto.CourseUpdateDTO;
import team.seekers.hula.course.course.entity.dto.CourseUpdateLogDTO;
import team.seekers.hula.course.course.entity.dto.bind.*;
import team.seekers.hula.course.course.entity.vo.CourseManageListVO;
import team.seekers.hula.course.course.service.CourseManageService;
import team.seekers.hula.course.resource.exam.dao.ExamDao;
import team.seekers.hula.course.resource.sectionContent.entity.dto.RollBackSectionContentDTO;
import team.seekers.hula.course.resource.sectionContent.entity.dto.SectionContentDTO;
import team.seekers.hula.course.resource.sectionContent.service.SectionContentManageService;
import team.seekers.hula.course.structure.chapter.dao.ChapterDao;
import team.seekers.hula.course.structure.chapter.entity.dao.ChapterDO;
import team.seekers.hula.course.structure.section.dao.SectionDao;
import team.seekers.hula.course.structure.section.entity.dao.SectionDO;
import team.seekers.hula.course.structure.section.service.SectionService;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Controller
@ApiResponseBody
@RequestMapping("/manage/course")
public class CourseManageController {

    @Autowired
    private CourseManageService courseManageService;

    @Autowired
    private CourseDao courseDao;

    @Autowired
    private ChapterDao chapterDao;

    @Autowired
    private SectionDao sectionDao;

    @Autowired
    private SectionService sectionService;

    @Autowired
    private ExamDao examDao;

    @Autowired
    private SectionContentManageService sectionContentManageService;


    /**
     * 创建知桨
     */
    @PostMapping("/create")
    public String create(@RequestBody CourseCreateDTO reqDTO) {
        reqDTO.setUserId(MindUtils.getUserId());
        return courseManageService.createCourse(reqDTO);
    }

    /**
     * 删除知桨
     */
    @GetMapping("/delete")
    public void delete(@RequestParam String courseId) {
        courseManageService.delete(CourseUpdateDTO.builder().id(courseId).build());
    }

    /**
     * 发布知桨
     */
    @GetMapping("/publish")
    public void publish(@RequestParam String courseId) {
        CourseDO courseDO = CourseDO.builder().id(courseId).status(3).build();
        courseDao.updateById(courseDO);
    }

    /**
     * 已绑定的所有信息
     */
    @GetMapping("/bindAllInfo")
    public HashMap<String, Object> bindAllInfo(@RequestParam Long courseId) {
        List<ChapterDO> chapterDOS = chapterDao.lambdaQuery().select(
                ChapterDO::getId,
                ChapterDO::getSequence,
                ChapterDO::getName
        ).eq(ChapterDO::getCourseId, courseId).orderByAsc(ChapterDO::getSequence).list();

        ArrayList<CourseBindChapterInfoDTO> courseBindChapterDTOS = new ArrayList<>();
        for(int i = 0;i<chapterDOS.size();i++){

            CourseBindChapterInfoDTO courseBindChapterDTO = new CourseBindChapterInfoDTO();
            courseBindChapterDTO.setId(chapterDOS.get(i).getId());
            courseBindChapterDTO.setName(chapterDOS.get(i).getName());
            courseBindChapterDTO.setOrder(chapterDOS.get(i).getSequence());

            // 获取所有的节
            List<SectionDO> list = sectionDao.lambdaQuery().select(
                    SectionDO::getId,
                    SectionDO::getName,
                    SectionDO::getChapterId
            ).orderByAsc(SectionDO::getSequence).eq(SectionDO::getChapterId, chapterDOS.get(i).getId()).list();
            ArrayList<CourseBindSectionInfoDTO> courseBindSectionInfoDTOS = new ArrayList<>();
            for (int j = 0;j<list.size();j++){
                CourseBindSectionInfoDTO courseBindSectionInfoDTO = new CourseBindSectionInfoDTO();
                courseBindSectionInfoDTO.setSectionId(list.get(j).getId());
                courseBindSectionInfoDTO.setName((i+1)+"-"+(j+1)+list.get(j).getName());

                List<SectionContentDTO> sectionContentDTOS = sectionContentManageService.querySectionContentDTOsBySectionId(list.get(j).getId());
                courseBindSectionInfoDTO.setSectionContents(sectionContentDTOS);
                courseBindSectionInfoDTOS.add(courseBindSectionInfoDTO);
            }
            courseBindChapterDTO.setSections(courseBindSectionInfoDTOS);
            courseBindChapterDTOS.add(courseBindChapterDTO);
        }

        HashMap<String, Object> result = new HashMap<>();
        result.put("chapters",courseBindChapterDTOS);

        return result;
    }

    /**
     * 知桨基本信息更新
     */
    @PostMapping("/update")
    public void update(@RequestBody @Valid CourseUpdateDTO reqDTO) {
        courseManageService.updateCourse(reqDTO);
    }

    /**
     * 管理员知桨分页查询
     */
    @GetMapping("/page")
    public PageResult<CourseUpdateDTO> page(CourseListReqDTO reqDTO) {
        return courseManageService.page(reqDTO);
    }


    /**
     * 管理页简洁课程分页查询
     */
    @GetMapping("/list")
    public PageResult<CourseManageListVO> miniPage(CourseListReqDTO reqDTO) {
        return courseManageService.list(reqDTO);
    }

    @GetMapping("/info")
    public CourseManageInfoVO query(@RequestParam String courseId) {
        return courseManageService.queryById(courseId);
    }

    /**
     * 我管理的所有知桨
     */
    @GetMapping("/list/manage")
    public List<CourseManageListVO> listMyAdmin() {
        return courseManageService.listMyAdmin(MindUtils.getUserId());
    }


    /**
     * 回滚对知桨内容的删除操作
     */
    @PostMapping("/rollback/delete")
    public void rollBackDelete(@RequestBody CourseUpdateLogDTO logDTO) {
        courseManageService.rollBackDelete(logDTO);
    }

    /**
     * 回滚对sectionContent的操作
     */

    @PostMapping("/rollback/sectionContent")
    public void rollBackSectionContent(@RequestBody RollBackSectionContentDTO sectionContentDTO){
        courseManageService.rollBackSectionContent(sectionContentDTO);
    }




//    @PostMapping("/bindChapters")
//    public Void bindChapters(@RequestBody @Valid CourseBindDTO reqDTO) {
//        // 删除所有已存在的
//        chapterDao.lambdaUpdate().eq(ChapterDO::getCourseId,reqDTO.getCourseId()).remove();
//        sectionDao.lambdaUpdate().eq(SectionDO::getCourseId,reqDTO.getCourseId()).remove();
//        examDao.lambdaUpdate().eq(ExamDO::getCourseId,reqDTO.getCourseId()).remove();
//
//        String courseId = reqDTO.getCourseId();
//        List<CourseBindChapterDTO> chapters = reqDTO.getChapters();
//        ArrayList<String> chapterIds = new ArrayList<>();
//        for (CourseBindChapterDTO chapter : chapters) {
//            ChapterDO chapterDO = new ChapterDO();
//            chapterDO.setCourseId(courseId);
//            chapterDO.setName(chapter.getName());
//            chapterDO.setSequence(chapter.getOrder());
//            chapterDao.save(chapterDO);
//            String chapterId = chapterDO.getId();
//            chapterIds.add(chapterId);
//
//            ArrayList<String> sectionIds = new ArrayList<>();
//            if(chapter.getSections() != null && chapter.getSections().size() > 0){
//                for (ChapterBindSectionDTO section : chapter.getSections()) {
//                    SectionDO sectionDO = new SectionDO();
//                    sectionDO.setCourseId(courseId);
//                    sectionDO.setChapterId(chapterId);
//                    sectionDO.setSequence(section.getOrder());
//                    sectionDO.setName(section.getName());
//                    sectionDao.save(sectionDO);
//                    sectionIds.add(sectionDO.getId());
//                }
//            }
//
//            if(chapter.getExams() != null && chapter.getExams().size() > 0){
//                for (ChapterBindExamDTO exam : chapter.getExams()) {
//
//                }
//            }
//            chapterDao.updateById(chapterDO);
//        }
//
//        CourseDO courseDO = new CourseDO();
//        courseDO.setId(courseId);
//        courseDao.updateById(courseDO);
//
//        return null;
//    }
//
//    @PostMapping("/bindChaptersAndNext")
//    public Void bindChaptersAndNext(@RequestBody @Valid CourseBindDTO reqDTO) {
//        // 删除所有已存在的
//        chapterDao.lambdaUpdate().eq(ChapterDO::getCourseId,reqDTO.getCourseId()).remove();
//        sectionDao.lambdaUpdate().eq(SectionDO::getCourseId,reqDTO.getCourseId()).remove();
//        examDao.lambdaUpdate().eq(ExamDO::getCourseId,reqDTO.getCourseId()).remove();
//
//        String courseId = reqDTO.getCourseId();
//        List<CourseBindChapterDTO> chapters = reqDTO.getChapters();
//        ArrayList<String> chapterIds = new ArrayList<>();
//        for (CourseBindChapterDTO chapter : chapters) {
//            ChapterDO chapterDO = new ChapterDO();
//            chapterDO.setCourseId(courseId);
//            chapterDO.setName(chapter.getName());
//            chapterDO.setSequence(chapter.getOrder());
//            chapterDao.save(chapterDO);
//            String chapterId = chapterDO.getId();
//            chapterIds.add(chapterId);
//
//            ArrayList<String> sectionIds = new ArrayList<>();
//            if(chapter.getSections() != null && chapter.getSections().size() > 0){
//                for (ChapterBindSectionDTO section : chapter.getSections()) {
//                    SectionDO sectionDO = new SectionDO();
//                    sectionDO.setCourseId(courseId);
//                    sectionDO.setChapterId(chapterId);
//                    sectionDO.setSequence(section.getOrder());
//                    sectionDO.setName(section.getName());
//                    sectionDao.save(sectionDO);
//                    sectionIds.add(sectionDO.getId());
//                }
//            }
//
//            if(chapter.getExams() != null && chapter.getExams().size() > 0){
//                for (ChapterBindExamDTO exam : chapter.getExams()) {
//
//                }
//            }
//
////            chapterDO.setSections(BaseConvertUtils.longListToBytes(sectionIds));
//            chapterDao.updateById(chapterDO);
//        }
//
//        CourseDO courseDO = new CourseDO();
//        courseDO.setId(courseId);
//        courseDO.setStatus(1);
////        courseDO.setChapters(BaseConvertUtils.longListToBytes(chapterIds));
//        courseDao.updateById(courseDO);
//
//        return null;
//    }
//
//    @GetMapping("/bindChapterInfo")
//    public List<CourseBindChapterDTO> bindChapterInfo(@RequestParam Long courseId) {
//        List<ChapterDO> chapterDOS = chapterDao.lambdaQuery().select(
//                ChapterDO::getId,
//                ChapterDO::getName,
//                ChapterDO::getSequence
//        ).eq(ChapterDO::getCourseId, courseId).orderByAsc(ChapterDO::getSequence).list();
//
//        ArrayList<CourseBindChapterDTO> courseBindChapterDTOS = new ArrayList<>();
//        for (ChapterDO chapterDO : chapterDOS) {
//            CourseBindChapterDTO courseBindChapterDTO = new CourseBindChapterDTO();
//            courseBindChapterDTO.setName(chapterDO.getName());
//            courseBindChapterDTO.setOrder(chapterDO.getSequence());
//
//            List<SectionDO> list = sectionDao.lambdaQuery().select(
//                    SectionDO::getId,
//                    SectionDO::getName,
//                    SectionDO::getSequence
//            ).orderByAsc(SectionDO::getSequence).eq(SectionDO::getChapterId, chapterDO.getId()).list();
//
//            ArrayList<ChapterBindSectionDTO> chapterBindSectionDTOS = new ArrayList<>();
//            for (SectionDO sectionDO : list) {
//                ChapterBindSectionDTO chapterBindSectionDTO = new ChapterBindSectionDTO();
//                chapterBindSectionDTO.setName(sectionDO.getName());
//                chapterBindSectionDTO.setOrder(sectionDO.getSequence());
//                chapterBindSectionDTOS.add(chapterBindSectionDTO);
//            }
//            courseBindChapterDTO.setSections(chapterBindSectionDTOS);
//            courseBindChapterDTOS.add(courseBindChapterDTO);
//        }
//
//        return courseBindChapterDTOS;
//    }
//
//    @GetMapping("/bindSectionInfo")
//    public List<CourseBindSectionInfoDTO> bindSectionInfo(@RequestParam String courseId) {
//        List<SectionDO> sectionDOS = sectionService.getSectionDOsByCourseId(courseId);
//
//
//        ArrayList<CourseBindSectionInfoDTO> courseBindSectionInfoDTOS = new ArrayList<>();
//        for(int i = 0;i<sectionDOS.size();i++){
//            List<SectionDO> list = sectionDao.lambdaQuery().select(
//                    SectionDO::getId,
//                    SectionDO::getName,
//                    SectionDO::getChapterId
//            ).orderByAsc(SectionDO::getSequence).eq(SectionDO::getChapterId, sectionDOS.get(i)).list();
//            for (int j = 0;j<list.size();j++){
//                CourseBindSectionInfoDTO courseBindSectionInfoDTO = new CourseBindSectionInfoDTO();
//                courseBindSectionInfoDTO.setSectionId(list.get(j).getId());
//                courseBindSectionInfoDTO.setName((i+1)+"-"+(j+1)+list.get(j).getName());
//
//                List<SectionContentDTO> sectionContentDTOS = sectionContentManageService.querySectionContentDTOsBySectionId(list.get(j).getId());
//                courseBindSectionInfoDTO.setSectionContents(sectionContentDTOS);
//                courseBindSectionInfoDTOS.add(courseBindSectionInfoDTO);
//            }
//        }
//        return courseBindSectionInfoDTOS;
//    }
}
